app.controller('list_ctrl',['$scope','$http','$location','$rootScope','setMenuActive',function($scope,$http,$location,$rootScope,setMenuActive){//列表显示控制器
	$scope.listfn=function(){
		$http({
			method:"get",
			url:"api/api.php"
		}).success(function(data){
			$scope.list=data
		})
	}
	$scope.listfn();	
	$rootScope.navliststr=setMenuActive($location);
}]);

app.controller('add_ctrl',['$scope','$http','$route','$location','$rootScope','setMenuActive',function($scope,$http,$route,$location,$rootScope,setMenuActive){//添加和编辑用户信息控制器
	if(typeof $location.search().id !="undefined"){//编辑页面，判断地址栏参数里面有没有id这一项，如果没有，表明是添加的操作，如果有，表明是编辑的操作
		$scope.tit="修改";
		$http({//用来请求列表数据
			method:"get",
			url:"api/api.php",
			params:{type:"get",id:$location.search().id}
		}).success(function(data){
				if(data.length>0){					
					$scope.usrname=data[0].username;
					$scope.usrpwd=data[0].password;
				}
		})
		var arr=setMenuActive($location);//setMenuActive
		arr[2].isactive=true;
		$rootScope.navliststr=arr;
	}else{//添加页面
		$scope.usrname=$scope.usrpwd="";
		$scope.tit="添加";
		$rootScope.navliststr=setMenuActive($location);
	}
	
	$scope.addfn=function(){//点击添加或者编辑按钮
		if($scope.usrname==""){
			layer.msg('用户名不为空');
			return;
		};
		if($scope.usrpwd==""){
			layer.msg('密码不为空');
			return;
		};
		if(typeof $location.search().id !="undefined"){//编辑
			$http({//修改接口请求
				method:"get",
				url:"api/api.php?type=update",
				params:{username:$scope.usrname,password:$scope.usrpwd,id:$location.search().id}
			}).success(function(data){
				if(data.result=="1"){
					layer.msg('修改成功');
					$scope.usrname=$scope.usrpwd="";
				}					
			})
		}else{
			$http({//添加接口请求
				method:"get",
				url:"api/api.php",
				params:{type:"add",username:$scope.usrname,password:$scope.usrpwd}
			}).success(function(data){
				if(data.result=="1"){
					layer.msg('添加成功');
					$scope.usrname=$scope.usrpwd="";
				}					
			})
		}
		
	}
	$scope.backlist=function(){//返回列表
		$location.url("");
	}
	
	
}]);
app.controller('manage_ctrl',['$scope','$http','$route','$log','$location','setMenuActive','$rootScope',function($scope,$http,$route,$log,$location,setMenuActive,$rootScope){//管理用户信息控制器
	$scope.listfn=function(){
		$http({
			method:"get",
			url:"api/api.php"
		}).success(function(data){
			$scope.list=data;
		})
	}
	$scope.ischecked=true;
	$scope.allischecked=false;
	$scope.allcheck=function(){//全选
		$scope.allischecked=!$scope.allischecked;
		for (var x in $scope.list) {
			$scope.list[x].ischecked=$scope.allischecked
		}
		
	}
	$scope.isallcheck=function(){//检测是不是全选/全不选的状态
		var checknum=0;
		for(var x in $scope.list){
			if($scope.list[x].ischecked) checknum++;
		}
		$scope.allischecked = checknum==$scope.list.length;
	}
	$scope.reverseallcheck=function(){//反选
		for (var x in $scope.list) {
			$scope.list[x].ischecked=!$scope.list[x].ischecked;
		}
		$scope.isallcheck();
	}
	$scope.itemchecked=function(item){//单项的选择
		item.ischecked=!item.ischecked;
		$scope.isallcheck();
	}
	$scope.delete=function(dataid){//删除指定id的对应数据,dataid为点击的某一条数据的id
		//询问框
		layer.confirm('确定删除此条信息？', {
		  btn: ['删除','取消'] //按钮
		}, function(){
			$http({//删除操作接口请求
				method:"get",
				url:"api/api.php",
				params:{type:"del",id:dataid}
			}).success(function(data){
				if(data.result=="1"){
					layer.msg('删除成功', {icon: 1});
					// $route.reload();
					$scope.listfn();
				}
			})
		}, function(){});		
	};
	$scope.deleteAll=function(){//删除多条
		var arr=[];
		for (var x in $scope.list) {
			if($scope.list[x].ischecked) arr.push($scope.list[x].Id)
		}
		//询问框
		layer.confirm('确定删除这'+arr.length+'条信息？', {
		  btn: ['删除','取消'] //按钮
		}, function(){
			$http({//删除操作接口请求
				method:"get",
				url:"api/api.php",
				params:{type:"del",id:arr.join(",")}
			}).success(function(data){
				if(data.result=="1"){
					layer.msg('删除成功', {icon: 1});
					// $route.reload();
					$scope.listfn();
				}
			})
		}, function(){});		
	}
	$scope.edit=function(dataid){//设置编辑跳转
		$location.url('edit'+'?id='+dataid);
	}
	$scope.listfn();
	$rootScope.navliststr=setMenuActive($location);
}])

